package property.service.impl;

import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSession;
import property.mapper.FeedbackMapper;
import property.pojo.Feedback;
import property.service.FeedbackService;
import property.utils.mapper.MyBatisUtil;

public class FeedbackServiceImpl implements FeedbackService {
    @Override
    public int addFeedback(Feedback feedback) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        FeedbackMapper mapper = sqlSession.getMapper(FeedbackMapper.class);
        int i = mapper.insertSelective(feedback);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

    @Override
    public int deleteFeedback(Integer id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        FeedbackMapper mapper = sqlSession.getMapper(FeedbackMapper.class);
        int i;
        try {
            i = mapper.deleteByPrimaryKey(id);
        } catch (PersistenceException e){
            i = -1;
        }
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

    @Override
    public int updateFeedback(Feedback feedback) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        FeedbackMapper mapper = sqlSession.getMapper(FeedbackMapper.class);
        int i;
        try {
            i = mapper.updateByPrimaryKeySelective(feedback);
        } catch (PersistenceException e){
            i = -1;
        }
        sqlSession.commit();
        sqlSession.close();
        return i;
    }


}
